package com.hotel.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hotel.bean.Checkin;
import com.hotel.bean.Regist;
import com.hotel.dao.CheckinDao;
import com.hotel.dao.impl.CheckinDaoImpl;
import com.hotel.service.UserService;
import com.hotel.service.impl.UserServiceImpl;
import com.hotel.util.BaseServlet;
import com.hotel.util.CustomBeanUtils;

@WebServlet("/ShopServlet")
public class ShopServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	
	public String buy1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		
		// 接收请求商品信息
		String pid = request.getParameter("pid");
		String hid = request.getParameter("hid");
		request.getSession().setAttribute("pid", pid);
		request.getSession().setAttribute("hid", hid);
		
		return "/user/shop.jsp";
		
		
	}
	public String buy2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		HttpSession session = request.getSession();
		int pid = Integer.valueOf((String) session.getAttribute("pid"));
		int hid = Integer.valueOf((String)session.getAttribute("hid"));
		
		// 接受页面的数据
		Map<String, String[]> parameterMap = request.getParameterMap();

		Checkin checkin = CustomBeanUtils.populate(Checkin.class, parameterMap);

		CheckinDao cd = new CheckinDaoImpl();
		boolean success = cd.update(checkin, pid, hid);
		if (success) {
			response.sendRedirect(request.getContextPath() + "/ShopServlet?action=success");
		} else {
			response.sendRedirect(request.getContextPath() + "/UserServlet?action=fail");
		}

		return null;

	}
	public String success(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		
		return "/user/success.jsp";
		
		
	}
	
	public String fail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		
		return "/user/fail.jsp";
		
		
	}

}
